<template>
  <frame-view>
    <!-- 状态栏 -->
    <status-bar></status-bar>
    <!-- 标题栏 -->
    <title-bar
      :titleProp="$t('deodorize.photocatalystDeodorize')"
      :pageTypeProp="2"
      @clickGoBack="goBack"
    ></title-bar>
    <!-- 滚动栏 -->
    <scroll-view>
      <second-page-margin :deviceTypePro="this.deviceType">
        <card-divider :heightPro="8"></card-divider>
        <second-card>
          <!-- 除臭模式选择 -->
          <CardItemStateView
            :titlePro="getDeodorizeModeSelectTitle"
            :stateValuePro="getDeodorizeModeSelectState"
            :darkModePro="this.darkMode"
            @click="clickDeodorizeModeSelect"
          >
          </CardItemStateView>
          <second-card-divider />
          <!-- 定时模式设置 -->
          <CardItemStateView
            :titlePro="getDeodorizeTimeRangeTitle"
            :stateValuePro="getDeodorizeTimeRangeState"
            :darkModePro="this.darkMode"
            @click="clickDeodorizeTimeRange"
          ></CardItemStateView>
          <second-card-divider />
          <!-- 定点模式设置 -->
          <CardItemStateView
            :titlePro="getDeodorizeTimeTitle"
            :stateValuePro="getDeodorizeTimeState"
            :darkModePro="this.darkMode"
            @click="clickDeodorizeTime"
          ></CardItemStateView>
          <second-card-divider />
          <!-- 除臭时长设置 -->
          <CardItemStateView
            :titlePro="getDeodorizeDurationTitle"
            :stateValuePro="getDeodorizeDurationState"
            :darkModePro="this.darkMode"
            @click="clickDeodorizDuration"
          ></CardItemStateView>
        </second-card>
        <card-divider></card-divider>
        <card-divider :heightPro="30"></card-divider>
      </second-page-margin>
    </scroll-view>

    <!-- 除臭模式选择对话框 -->
    <select-list-dialog
      :deviceTypePro="this.deviceType"
      :listData="deodorizeModeSelectList"
      :darkModePro="this.darkMode"
      :selectedIndexPro="getDeodorizeModeSelectSelectedIndex"
      :selectVisiblePro="this.deodorizeModeSelectDialogVisible"
      :selectVisible.sync="deodorizeModeSelectDialogVisible"
      @clickItem="clickDeodorizeModeSelectItem"
    ></select-list-dialog>
    <!-- 除臭时长选择对话框 -->
    <select-list-dialog
      :deviceTypePro="this.deviceType"
      :listData="deodorizeDurationList"
      :darkModePro="this.darkMode"
      :selectedIndexPro="getDeodorizeDurationSelectedIndex"
      :selectVisiblePro="this.deodorizeDurationDialogVisible"
      :selectVisible.sync="deodorizeDurationDialogVisible"
      @clickItem="clickDeodorizeDurationItem"
    ></select-list-dialog>

    <!-- 除臭定时选择对话框 -->
    <PickerDialog
      :deviceTypePro="this.deviceType"
      :showTitlePro="$t('deodorize.timeRangeModeSet')"
      :data="deodorizeTimeRangePickerData"
      :defaultIndex="getDeodorizeTimeRangePickerSelectedIndex"
      :darkModePro="this.darkMode"
      @cancel="deodorizeTimeRangePickerCancel"
      @confirm="deodorizeTimeRangePickerConfirm"
      :visible.sync="deodorizeTimeRangePickerDialogVisible"
    />
    <!-- 除臭定点选择对话框 -->
    <PickerDialog
      :deviceTypePro="this.deviceType"
      :showTitlePro="$t('deodorize.timeModeSet')"
      :data="deodorizeTimePickerData"
      :defaultIndex="getDeodorizeTimePickerSelectedIndex"
      :darkModePro="this.darkMode"
      @cancel="deodorizeTimePickerCancel"
      @confirm="deodorizeTimePickerConfirm"
      :visible.sync="deodorizeTimePickerDialogVisible"
    />
  </frame-view>
</template>
<script>
import "../assets/css/setting.css";
import "../assets/css/frame.css";

import {
  refreshWebView,
  modifyTitleBar,
  setChangeTitleStyle,
  overrideTitleBarRightBtn,
  setgoSettingListener,
  setMessageVisible,
  getStatusBarHeight,
  setStatuBarHeightListener,
  sendProperty,
  sendProperties,
  setPropertiesListener,
  setNavigationVisible,
  getDevCacheAll,
  getProperty,
  toast,
  debugLog,
  userLog,
  getDarkMode,
  finishDeviceActivity,
  deleteDevice,
  modifyDeviceName,
  modifyDeviceNameById,
  modifyRoomName,
  getDeviceInfo,
  setDeviceInfoListener,
  getDevInfoAll,
  setDevInfoAllListener,
  setDeleteDeviceCallBack,
  setModifyDeviceNameCallBack,
  setModifyDeviceNameByIdCallBack,
  setModifyRoomCallBack,
  setSendPropertyListener,
  setSendPropertiesListener,
  setRequestPropertyListener,
  setOnResumeListener,
  setOnPauseListener,
  createRule,
  setCreateRuleListener,
  deleteRule,
  setDeleteRuleListener,
  getRules,
  setGetRuleListener,
  controlRuleActive,
  setControlRuleActiveListener,
  jumpToSetting,
  setJumpToSettingListener,
  getSwitchProperties,
  setRequestSwitchPropertiesListener,
  getControlProperties,
  setRequestControlPropertiesListener,
  getStatusProperties,
  setRequestStatusPropertiesListener,
  getGearProperties,
  setRequestGearPropertiesListener,
  getWarnEventProperties,
  setRequestWarnEventPropertiesListener,
  getFaultEventProperties,
  setRequestFaultEventPropertiesListener,
  isScreenSpreaded,
  getCurrentTime,
} from "../assets/js/device.js";
import {
  WIDTH_PHONE,
  PHONE_RATIO,
  WIDTH_SPREADED,
  SPREADED_RATIO,
  WIDTH_PAD_PORTRAIT,
  PAD_PORTRAIT_RATIO,
  WIDTH_PAD_LANDSCAPE,
  PAD_LANDSCAPE_RATIO,
  WIDTH_STANDARD,
  SPREADED_SCREEN_RATIO,
  selectWidth,
  flexibleWidth,
  flexibleHeight,
  screenDpr,
  screenType,
  screenDescription,
  isIOS,
  isSpreadOn,
  screenSize,
  getSecondPageMarginStyle,
  getSecondPageStyle,
  getDialogFlexAlign,
  getDialogStyle,
  getSingleButtonStyle,
  setScreenResizeListener,
  itemWidth,
} from "../assets/js/flexible.js";

import Const from "../const/const.js";

import FrameView from "./custom/frame/FrameView.vue";
import ScrollView from "./custom/frame/ScrollView.vue";
import SecondPageMargin from "./custom/frame/second/SecondPageMargin.vue";
import SecondCard from "./custom/frame/second/SecondCard.vue";
import SecondCardDivider from "./custom/frame/second/SecondCardDivider.vue";
import StatusBar from "./custom/frame/StatusBar.vue";
import TitleBar from "./custom/frame/TitleBar.vue";
import CardDivider from "./custom/CardDivider.vue";
import MySwitch from "./custom/MySwitch.vue";
import CardItemSwitchView from "./custom/setting/CardItemSwitchView.vue";
import CardItemStateView from "./custom/setting/CardItemStateView.vue";
import CardItemDoubleLineSwitchView from "./custom/setting/CardItemDoubleLineSwitchView.vue";
import DialogContainer from "./custom/dialog/DialogContainer.vue";
import DialogTitleView from "./custom/dialog/DialogTitleView.vue";
import DialogConfirmView from "./custom/dialog/DialogConfirmView.vue";
import SelectListDialog from "./custom/dialog/selectlist/SelectListDialog.vue";
import PickerDialog from "./custom/picker/PickerDialog.vue";

import eventBus from "../assets/js/eventbus.js";

// 除臭定时picker数据
let deodorizeTimeRangeMaxValue = 144;
let deodorizeTimeRangeMinValue = 12;
let deoderizeTimeRangeNum = 7; //每组数据个数
let deodorizeTimeRangeRepeatNum = 16; //数据重复的遍数
var deodorizeTimeRangePickerArray = [];

//除臭定点时钟picker数据
let deodorizeTimeHourMaxValue = 23;
let deodorizeTimeHourMinValue = 0;
let deoderizeTimeHourNum = 24; //每组数据个数
let deodorizeTimeHourRepeatNum = 8; //数据重复的遍数
var deodorizeTimeHourPickerArray = [];

//除臭定点分钟picker数据
let deodorizeTimeMinuteMaxValue = 59;
let deodorizeTimeMinuteMinValue = 0;
let deoderizeTimeMinuteNum = 60; //每组数据个数
let deodorizeTimeMinuteRepeatNum = 8; //数据重复的遍数
var deodorizeTimeMinutePickerArray = [];

export default {
  name: "Setting",
  components: {
    FrameView,
    ScrollView,
    SecondCardDivider,
    SecondCard,
    SecondPageMargin,
    StatusBar,
    TitleBar,
    CardDivider,
    MySwitch,
    CardItemSwitchView,
    CardItemStateView,
    CardItemDoubleLineSwitchView,
    DialogContainer,
    DialogTitleView,
    DialogConfirmView,
    SelectListDialog,
    PickerDialog,
  },
  data() {
    return {
      darkMode: "light", //暗黑/白天模式
      deviceType: 1,

      deodorizeModeSelectValue: 0, //除臭模式选择值：0-关闭 1-定点模式 2-定时模式 3-自动模式
      deodorizeModeSelectState: "",
      deodorizeModeSelectDialogVisible: false, //除臭模式选择对话框是否可见

      deodorizeDurationValue: 0, //除臭时长：0-5分钟 1-10分钟 2-15分钟 3-20分钟 4-25分钟 5-30分钟
      deodorizeDurationState: "",
      deodorizeDurationDialogVisible: false, //除臭时长对话框是否可见

      deodorizeModeSelectList: {
        title: "",
        selectedIndex: -1,
        values: [],
        indexs: [],
        list: [],
        itemType: [],
        subtitleList: [],
      }, //除臭模式选择选项列表
      deodorizeDurationList: {
        title: "",
        selectedIndex: -1,
        values: [],
        indexs: [],
        list: [],
      }, //除臭时长选项列表

      deodorizeTimeRangePickerData: [deodorizeTimeRangePickerArray], //除臭定时选择框列表数据
      deodorizeTimeRangeValue: 0, //除臭定时时长：0-12小时 1-24小时 2-48小时 3-72小时 4-96小时 5-120小时 6-144小时
      deodorizeTimeRangePickerDialogVisible: false,

      deodorizeTimePickerData: [
        deodorizeTimeHourPickerArray,
        deodorizeTimeMinutePickerArray,
      ],
      deodorizeTimeHourValue: 8, //除臭定点时钟值：0-23时
      deodorizeTimeMinuteValue: 0, //除臭定点分钟值：0-59时
      deodorizeTimePickerDialogVisible: false,

      //缓存数据
      settingData: null,
      toiletGearData: null,

      uiLockTimer: "", //定时器：发送指令后，锁定UI不更新3秒钟，避免状态错误
      uiIsLocking: false, //锁：发送指令后，锁定UI不更新3秒钟，避免状态错误
    };
  },

  created() {
    /*------------------------设置监听器（必须放在前面）-------------------------*/
    //设置发送指令的回调函数
    setSendPropertyListener(this.sendPropertyListener);
    //设置发送多条指令的回调函数
    setSendPropertiesListener(this.sendPropertyListener);
    //设备属性事件监听器
    setPropertiesListener(this.propertiesListener);
    /*----------------------------------------------------------------------*/

    //设置标题栏可见性
    setNavigationVisible(false);
    if (getDarkMode() === 2) {
      modifyTitleBar(true);
      this.darkMode = "dark";
    } else {
      modifyTitleBar(false);
      this.darkMode = "light";
    }

    //获取设备所有属性参数（旧接口）
    // setDeviceInfoListener(this.updateDeviceInfo);
    //获取设备所有属性参数（旧接口）
    // getDeviceInfo();
    //获取设备所有属性参数（新接口）
    setDevInfoAllListener(this.updateDevInfoAll);
    getDevInfoAll();
  },

  mounted() {
    //屏幕类型
    this.deviceType = screenType;
    //监听屏幕尺寸变化
    setScreenResizeListener(this.updateScreenResize);

    //添加监听器：右滑或点击系统返回键关闭页面
    if (window.history && window.history.pushState) {
      history.pushState(null, null, document.URL);
      window.addEventListener("popstate", this.goBack, false);
    }

    // 初始化除臭模式选择对话框列表
    this.deodorizeModeSelectList = {
      title: this.$i18n.t("deodorize.deodorizeModeSelect"),
      values: [2, 1, 3, 0],
      indexs: [3, 1, 0, 2],
      selectedIndex: 2,
      list: [
        this.$i18n.t("deodorize.timeRangeMode"),
        this.$i18n.t("deodorize.timeMode"),
        this.$i18n.t("deodorize.autoMode"),
        this.$i18n.t("deodorize.closeMode"),
      ],
      subtitleList: [
        this.$i18n.t("deodorize.timeRangeModeDesc"),
        this.$i18n.t("deodorize.timeModeDesc"),
        this.$i18n.t("deodorize.autoModeDesc"),
        "",
      ],
      itemType: [1, 1, 1, 0],
    };
    // 初始化除臭时长对话框列表
    this.deodorizeDurationList = {
      title: this.$i18n.t("deodorize.deodorizeTimeSet"),
      values: [0, 1, 2, 3, 4, 5], //传入列表的索引值，获取对应的属性值
      indexs: [0, 1, 2, 3, 4, 5], //传入收到的属性值，获取列表对应索引值
      selectedIndex: 1,
      list: [
        "5" + this.$i18n.t("deodorize.minute"),
        "10" + this.$i18n.t("deodorize.minute"),
        "15" + this.$i18n.t("deodorize.minute"),
        "20" + this.$i18n.t("deodorize.minute"),
        "25" + this.$i18n.t("deodorize.minute"),
        "30" + this.$i18n.t("deodorize.minute"),
      ],
    };
    // 初始化除臭定时模式对话框列表
    for (var i = 0; i < deodorizeTimeRangeRepeatNum; i++) {
      deodorizeTimeRangePickerArray.push({
        min: deodorizeTimeRangeMinValue,
        max: deodorizeTimeRangeMaxValue,
        label: 12,
        value: 12,
        unit: this.$i18n.t("deodorize.hour"),
      });
      deodorizeTimeRangePickerArray.push({
        min: deodorizeTimeRangeMinValue,
        max: deodorizeTimeRangeMaxValue,
        label: 24,
        value: 24,
        unit: this.$i18n.t("deodorize.hour"),
      });
      deodorizeTimeRangePickerArray.push({
        min: deodorizeTimeRangeMinValue,
        max: deodorizeTimeRangeMaxValue,
        label: 48,
        value: 48,
        unit: this.$i18n.t("deodorize.hour"),
      });
      deodorizeTimeRangePickerArray.push({
        min: deodorizeTimeRangeMinValue,
        max: deodorizeTimeRangeMaxValue,
        label: 72,
        value: 72,
        unit: this.$i18n.t("deodorize.hour"),
      });
      deodorizeTimeRangePickerArray.push({
        min: deodorizeTimeRangeMinValue,
        max: deodorizeTimeRangeMaxValue,
        label: 96,
        value: 96,
        unit: this.$i18n.t("deodorize.hour"),
      });
      deodorizeTimeRangePickerArray.push({
        min: deodorizeTimeRangeMinValue,
        max: deodorizeTimeRangeMaxValue,
        label: 120,
        value: 120,
        unit: this.$i18n.t("deodorize.hour"),
      });
      deodorizeTimeRangePickerArray.push({
        min: deodorizeTimeRangeMinValue,
        max: deodorizeTimeRangeMaxValue,
        label: 144,
        value: 144,
        unit: this.$i18n.t("deodorize.hour"),
      });
    }

    //添加除臭定点对话框时钟列表
    for (var i = 0; i < deodorizeTimeHourRepeatNum; i++) {
      //重复遍数
      for (var j = 0; j < deoderizeTimeHourNum; j++) {
        //每遍的数据个数
        deodorizeTimeHourPickerArray.push({
          min: deodorizeTimeHourMinValue,
          max: deodorizeTimeHourMaxValue,
          label: j,
          value: j,
          unit: this.$i18n.t("deodorize.hourShort"),
        });
      }
    }
    //添加除臭定点对话框分钟列表
    for (var i = 0; i < deodorizeTimeMinuteRepeatNum; i++) {
      //重复遍数
      for (var j = 0; j < deoderizeTimeMinuteNum; j++) {
        //每遍的数据个数
        deodorizeTimeMinutePickerArray.push({
          min: deodorizeTimeMinuteMinValue,
          max: deodorizeTimeMinuteMaxValue,
          label: j,
          value: j,
          unit: this.$i18n.t("deodorize.minuteShort"),
        });
      }
    }
  },

  activated() {
    //设备属性事件监听器
    setPropertiesListener(this.propertiesListener);
  },

  destroyed() {
    //移除监听器：右滑或点击系统返回键关闭页面
    window.removeEventListener("popstate", this.goBack, false);
  },

  watch: {},

  computed: {
    //除臭定点模式选择
    getDeodorizeTimeTitle() {
      return this.$i18n.t("deodorize.timeModeSet");
    },
    getDeodorizeTimeState() {
      return (
        (this.deodorizeTimeHourValue > 9
          ? this.deodorizeTimeHourValue
          : "0" + this.deodorizeTimeHourValue) +
        ":" +
        (this.deodorizeTimeMinuteValue > 9
          ? this.deodorizeTimeMinuteValue
          : "0" + this.deodorizeTimeMinuteValue)
      );
    },
    getDeodorizeTimePickerSelectedIndex() {
      console.log(
        "Page getDeodorizeTimePickerSelectedIndex(): selectIndex1 = " +
          (deoderizeTimeHourNum * (deodorizeTimeHourRepeatNum / 2) +
            this.deodorizeTimeHourValue)
      );
      console.log(
        "Page getDeodorizeTimePickerSelectedIndex(): selectIndex2 = " +
          (deoderizeTimeMinuteNum * (deodorizeTimeMinuteRepeatNum / 2) +
            this.deodorizeTimeMinuteValue)
      );
      return [
        deoderizeTimeHourNum * (deodorizeTimeHourRepeatNum / 2) +
          this.deodorizeTimeHourValue,
        deoderizeTimeMinuteNum * (deodorizeTimeMinuteRepeatNum / 2) +
          this.deodorizeTimeMinuteValue,
      ];
    },
    //除臭定时选择
    getDeodorizeTimeRangeTitle() {
      return this.$i18n.t("deodorize.timeRangeModeSet");
    },
    getDeodorizeTimeRangeState() {
      switch (this.deodorizeTimeRangeValue) {
        case 0:
          return 12 + this.$i18n.t("deodorize.hour");
        case 1:
          return 24 + this.$i18n.t("deodorize.hour");
        case 2:
          return 48 + this.$i18n.t("deodorize.hour");
        case 3:
          return 72 + this.$i18n.t("deodorize.hour");
        case 4:
          return 96 + this.$i18n.t("deodorize.hour");
        case 5:
          return 120 + this.$i18n.t("deodorize.hour");
        case 6:
          return 144 + this.$i18n.t("deodorize.hour");
      }
    },
    getDeodorizeTimeRangeLabelValue() {
      switch (this.deodorizeTimeRangeValue) {
        case 0:
          return 12;
        case 1:
          return 24;
        case 2:
          return 48;
        case 3:
          return 72;
        case 4:
          return 96;
        case 5:
          return 120;
        case 6:
          return 144;
      }
    },
    getDeodorizeTimeRangePickerSelectedIndex() {
      return (
        deoderizeTimeRangeNum * (deodorizeTimeRangeRepeatNum / 2) +
        this.deodorizeTimeRangeValue
      );
    },

    //除臭模式选择
    getDeodorizeModeSelectTitle() {
      return this.deodorizeModeSelectList.title;
    },
    getDeodorizeModeSelectState() {
      return this.deodorizeModeSelectList.list[
        this.deodorizeModeSelectList.selectedIndex
      ];
    },
    getDeodorizeModeSelectSelectedIndex() {
      return this.deodorizeModeSelectList.indexs[this.deodorizeModeSelectValue];
    },

    //除臭时长设置
    getDeodorizeDurationTitle() {
      return this.deodorizeDurationList.title;
    },
    getDeodorizeDurationState() {
      return this.deodorizeDurationList.list[
        this.deodorizeDurationList.selectedIndex
      ];
    },
    getDeodorizeDurationSelectedIndex() {
      return this.deodorizeDurationList.indexs[this.deodorizeDurationValue];
    },
  },

  methods: {
    //除臭定点设置
    clickDeodorizeTime() {
      this.deodorizeTimePickerDialogVisible = true;
    },
    deodorizeTimePickerCancel() {},
    deodorizeTimePickerConfirm(result, indexArr) {
      if (result !== undefined && result !== null) {
        if (result.length > 0) {
          if (result[0] != null && result[0].value !== undefined) {
            this.deodorizeTimeHourValue = result[0].value;
          }
        }
        if (result.length > 1) {
          if (result[1] != null && result[1].value !== undefined) {
            this.deodorizeTimeMinuteValue = result[1].value;
          }
        }
      }
      let data = [
        { toiletGear: { deodorizationHour: this.deodorizeTimeHourValue } },
        { toiletGear: { deodorizationMin: this.deodorizeTimeMinuteValue } },
      ];
      this.prepareSendProperties(data);
    },

    //除臭定时设置
    getDeodorizeTimeRangeValueFromLabel(labelValue) {
      switch (labelValue) {
        case 12:
          return 0;
        case 24:
          return 1;
        case 48:
          return 2;
        case 72:
          return 3;
        case 96:
          return 4;
        case 120:
          return 5;
        case 144:
          return 6;
        default:
          return 12;
      }
    },
    clickDeodorizeTimeRange() {
      this.deodorizeTimeRangePickerDialogVisible = true;
    },
    deodorizeTimeRangePickerCancel() {},
    deodorizeTimeRangePickerConfirm(result, indexArr) {
      // console.log("SettingPage confirm(): result[0] = " + result[0].value);
      // console.log("SettingPage confirm(): selectInd = " + indexArr[0]);
      this.deodorizeTimeRangeValue = this.getDeodorizeTimeRangeValueFromLabel(
        result[0].value
      );
      let data = {
        toiletGear: { regularDeodorization: this.deodorizeTimeRangeValue },
      };
      this.prepareSendProperty(data);
    },

    //点击除臭模式选择按钮
    clickDeodorizeModeSelect() {
      console.log("点击除臭模式选择按钮");
      this.deodorizeModeSelectDialogVisible = true;
    },
    //选中某个除臭模式
    clickDeodorizeModeSelectItem(key) {
      this.deodorizeModeSelectList.selectedIndex = key;
      this.deodorizeModeSelectState = this.deodorizeModeSelectList.list[key];
      this.deodorizeModeSelectValue = this.deodorizeModeSelectList.values[key];
      let data = { setting: { photocatalyst: this.deodorizeModeSelectValue } };
      this.prepareSendProperty(data);
    },

    //点击除臭时长设置按钮
    clickDeodorizDuration() {
      console.log("点击除臭时长设置按钮");
      this.deodorizeDurationDialogVisible = true;
    },
    //选中某个除臭时长
    clickDeodorizeDurationItem(key) {
      this.deodorizeDurationList.selectedIndex = key;
      this.deodorizeDurationState = this.deodorizeDurationList.list[key];
      this.deodorizeDurationValue = this.deodorizeDurationList.values[key];
      let data = {
        toiletGear: { deodorizationDuration: this.deodorizeDurationValue },
      };
      this.prepareSendProperty(data);
    },

    updateScreenResize(newScreenType) {
      this.deviceType = newScreenType;
    },
    //获取设备所有控制属性值的回调方法用于刷新UI
    updateDevInfoAll: function (data) {
      // toast(1000, "updateDevInfoAll");
      console.log("新接口getDevInfoAll：");
      console.log(data);
      if (data.services != null) {
        this.testSettingData = data;
        for (var i = 0; i < data.services.length; i++) {
          this.propertiesListener(data.services[i]);
        }
      }
    },

    //获取设备信息并刷新UI（初始化设备状态）
    updateDeviceInfo: function (data) {
      if (data.services != null) {
        for (var i = 0; i < data.services.length; i++) {
          this.propertiesListener(data.services[i]);
        }
      }
    },

    //发送指令
    prepareSendProperty: function (jsObject) {
      clearTimeout(this.uiLockTimer);
      this.uiIsLocking = true;
      this.uiLockTimer = setTimeout(() => {
        console.log("发送指令3s后更新UI");
        this.uiIsLocking = false;
        // this.updateUIFromData();
        getDevInfoAll();
      }, 3000);
      sendProperty(jsObject);
    },

    //发送多条指令
    prepareSendProperties: function (jsObject) {
      clearTimeout(this.uiLockTimer);
      this.uiIsLocking = true;
      this.uiLockTimer = setTimeout(() => {
        console.log("发送多指令3s后更新UI");
        this.uiIsLocking = false;
        // this.updateUIFromData();
        getDevInfoAll();
      }, 3000);
      sendProperties(jsObject);
    },

    //发送指令后的回调
    sendPropertyListener: function (data) {
      if (data.errcode === 0) {
        // toast(2000, "指令发送成功");
      } else {
        // toast(2000, "指令发送失败");
        this.updateUIFromData();
        console.log("指令发送失败");
      }
    },
    //用缓存更新UI
    updateUIFromData() {
      if (this.settingData != null) {
        this.updateUI(this.settingData);
      }
      if (this.toiletGearData != null) {
        this.updateUI(this.toiletGearData);
      }
    },

    // //用请求的服务数据更新UI
    // updateUIFromServiceData: function (data) {
    //   if (data.autoMagicBubble !== undefined) {
    //     this.paoValue = data.autoMagicBubble === 1;
    //   }
    //   if (data.autoFlip !== undefined) {
    //     this.coverValue = data.autoFlip === 1;
    //   }
    //   if (data.autoFlushing !== undefined) {
    //     this.flushValue = data.autoFlushing === 1;
    //   }
    //   if (data.tempSensor !== undefined) {
    //     this.tempValue = data.tempSensor === 1;
    //   }
    //   if (data.lightSense !== undefined) {
    //     this.lightValue = data.lightSense === 1;
    //   }
    //   if (data.footFeel !== undefined) {
    //     this.footValue = data.footFeel === 1;
    //   }
    // },

    //处理属性上报事件(处理设备上传的属性事件)
    propertiesListener: function (data) {
      if (data.sid === undefined || data.data === undefined) return;
      //先缓存数据
      if (data.sid === "setting") {
        this.settingData = data;
      }
      if (data.sid === "toiletGear") {
        this.toiletGearData = data;
      }
      //是否锁定UI更新
      if (this.uiIsLocking) {
        return;
      }
      this.updateUI(data);
    },
    //更新UI
    updateUI(data) {
      // 根据上报的数据，更新UI
      if (data.sid !== undefined && data.sid === "setting") {
        //除臭模式
        if (data.data.photocatalyst !== undefined) {
          this.deodorizeModeSelectValue = data.data.photocatalyst === 1;
        }
      } else if (data.sid !== undefined && data.sid === "toiletGear") {
        //除臭时长
        if (data.data.deodorizationDuration !== undefined) {
          this.deodorizeDurationValue = data.data.deodorizationDuration === 1;
        }
        //定时除臭
        if (data.data.regularDeodorization !== undefined) {
          this.deodorizeTimeRangeValue = data.data.regularDeodorization === 1;
        }
        //定点除臭小时
        if (data.data.deodorizationHour !== undefined) {
          this.deodorizeTimeHourValue = data.data.deodorizationHour === 1;
        }
        //定点除臭分钟
        if (data.data.deodorizationMin !== undefined) {
          this.deodorizeTimeMinuteValue = data.data.deodorizationMin === 1;
        }
      }
    },

    //返回前一个页面
    goBack: function () {
      //replace替换原路由，作用是避免回退死循环
      this.$router.replace({ path: "/" });
      //关闭页面返回上一级页面时，向上一级页面发送消息
      eventBus.$emit("SettingPage_goback", "我是设置页面的数据");
      // this.$router.go(-1);//添加右滑或点击系统返回键退出页面的监听器后，不能调用原路由返回
    },

    gotoMoreSetting: function () {
      //跳转到鸿蒙默认的设置页面
      console.log("更多设置");
      jumpToSetting();
    },
  },
};
</script>
<style scoped>
.settingSwitchItem {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  height: 48px;
  align-items: center;
}

.settingSwitchItem .title {
  font-size: 16px;
  color: var(--current-primary-color);
}

.settingSwitchItem .switch {
  margin-right: 0px;
  /* background-color: #dedede; */
}
</style>
